package cn.hk.recharge.controller;

import cn.HK.po.User;
import cn.HK.vo.SystemResult;
import cn.hk.recharge.service.RechargeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.logging.Logger;

@RestController
@RequestMapping("/user")
public class RechargeController {
    private Logger logger;

    /**
     * 开通vip,充值
     *请求路径	/user/recharge
     * 请求方式	get
     * 请求参数	uId
     * 返回值类型	SystemResult
     * @return
     */
    /*
    * 大腿的意思是：
    * 模拟直接支付成功，即点击vip就等于充钱了，改变用户vip状态

    * */
    @Autowired
    private RechargeService rechargeService;
    @RequestMapping(value = "/recharge", method = RequestMethod.GET)
    public SystemResult  recharge(Integer uId) {
        try{
            rechargeService.updatePoint(uId);
            return SystemResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SystemResult.build(201,"充值失败",null);
        }
   /* @Autowired
    private RechargeService rechargeService;
    @RequestMapping(value = "/recharge", method = RequestMethod.GET)
    public SystemResult  recharge( Double money, Integer uId) {

        try{
            Map<String, Object> result = rechargeService.recharge();
            if ("1".equals(result.get("status").toString())) {
                result=rechargeService.updaterecharge();
            }
            rechargeService.updatePoint(money,uId);
            return SystemResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SystemResult.build(201,"充值失败",null);
        }*/
        /*
    *     @RequestMapping("/getRechargePage")
    public String getRechargePage (String userId,Model model){
        System.out.println("充钱的的Userid为：" + userId);
        UserEntity user = userService.selectById(Integer.valueOf(userId));
        model.addAttribute("user",user);
        return "user/user_recharge";
    }

    @RequestMapping("/Recharge")
    @ResponseBody
     public  Result Recharge (UserEntity user){
        System.out.println("当前充钱的user信息 = " + user);
        UserEntity user1 = userService.selectById(user.getUid());
        System.out.println("原User的值 = " + user1 );
        UserEntity userNew = new UserEntity();
        userNew.setUid(user.getUid());
        userNew.setMoney(user.getMoney()+user1.getMoney());
        userNew.setBuyNumber(user1.getBuyNumber()+1);
        System.out.println("修改的User = " + userNew);
        //update修改就行
        updateUser(userNew);

        //直推奖励
        UserEntity userSup = userService.selectById(user.getUid());
        userSup.setMoney(userSup.getMoney() + user.getMoney()*0.015);
        //update修改
        updateUser(userSup);


        //团队奖励
        Integer count = Rechargedigui(user.getUid(),user.getMoney());
        System.out.println(count);

        if (count == 1){
            return Result.success("充值成功了");
        }else {
            return Result.error("充值异常");
        }
    }

    @RequestMapping("/Rechargedigui")
    public Integer Rechargedigui(Integer uSuperior,Double money){
        //进入修改
        Integer count = null;
        UserEntity user = userService.selectById(uSuperior);
        //查找到他父user
        System.out.println(user);

        if(user == null){
            System.out.println("没有父User了");
            return count = 0;
        }else {
           updateUser(user);
            count = Rechargedigui(user.getSuperior(),money);
        }
        return  count =1;

    }
*/



       /* Map<String, Object> result = RechargeService.recharge();
        if ("1".equals(result.get("status").toString())) {
            result=rechargeService.updaterecharge();
        }
        logger.info("`RechargeController/recharge()返回结果集：" + result);
        return SystemResult.build(201,"充值失败",null);*/
    }

}
